﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Entity;
using System.Web;
using System.Web.Mvc;
using HdipCricketClub.Models;
using HdipCricketClub.ViewModels;

namespace HdipCricketClub.ViewModels
{
    public class ScorecardEnterScoresViewModel
    {
        // properties

        public Fixture ScorecardFixture { get; set; }

        public ScorecardHeader ScorecardHeaderDetail { get; set; }

        public List<ClubFixturePlayer> ClubBatsmen { get; set; }

        public List<ClubFixturePlayer> ClubBowlers { get; set; }

        public ClubOtherFixtureDetail ClubOtherDetail { get; set; }

        public List<OpponentFixturePlayer> OpponentBatsmen { get; set; }

        public List<OpponentFixturePlayer> OpponentBowlers { get; set; }

        public OpponentOtherFixtureDetail OpponentOtherDetail { get; set; }

        // database context

        CricketClubEntities db = new CricketClubEntities();

        // repositories

        FixtureRepository fixtureRepository = new FixtureRepository();
        ClubFixturePlayerRepository clubFixturePlayerRepository = new ClubFixturePlayerRepository();
        OpponentFixturePlayerRepository opponentFixturePlayerRepository = new OpponentFixturePlayerRepository();

        // methods

        public ScorecardEnterScoresViewModel ParseInputData(FormCollection formCollection)
        {
            string[] fixtureId = formCollection.GetValues("ScorecardFixture.fixture_id");

            string[] resultId = formCollection.GetValues("ScorecardHeaderDetail.result_id");
            string[] wonToss = formCollection.GetValues("ScorecardHeaderDetail.won_toss");
            string[] batFirst = formCollection.GetValues("ScorecardHeaderDetail.batted_first");

            string[] clubPlayerFixtureId = formCollection.GetValues("item.fixture_id");
            string[] clubPlayerPlayerId = formCollection.GetValues("item.player_id");
            string[] clubPlayerBatsmanNo = formCollection.GetValues("item.batsman_number");
            string[] clubPlayerBowlerNo = formCollection.GetValues("item.bowler_number");

            string[] oppPlayerFixtureId = formCollection.GetValues("item.op_fixture_id");
            string[] oppPlayerBatsmanNo = formCollection.GetValues("item.op_batsman_number");
            string[] oppPlayerName = formCollection.GetValues("item.op_player_name");
            string[] oppPlayerBowlerNo = formCollection.GetValues("item.op_bowler_number");

            int scorecardFixtureId = Int32.Parse(fixtureId[0]);

            ScorecardHeader scorecardHeader = new ScorecardHeader()
                                            {
                                                fixture_id = scorecardFixtureId,
                                                result_id = Int32.Parse(resultId[0]),
                                                won_toss = bool.Parse(wonToss[0]),
                                                batted_first = bool.Parse(batFirst[0])
                                            };

            List<ClubFixturePlayer> clubBatsmenList = new List<ClubFixturePlayer>();
            List<ClubFixturePlayer> clubBowlersList = new List<ClubFixturePlayer>();

            for (int i = 0; i < clubPlayerPlayerId.Length; i++)
            {

                int thisFixtureId = Int32.Parse(clubPlayerFixtureId[i]);
                int thisPlayerId = Int32.Parse(clubPlayerPlayerId[i]);

                ClubFixturePlayer player = clubFixturePlayerRepository.GetClubFixturePlayer(thisFixtureId, thisPlayerId);

                if (clubPlayerBatsmanNo[i] != "")
                {
                    player.batsman_number = Int32.Parse(clubPlayerBatsmanNo[i]);
                }

                if (clubPlayerBowlerNo[i] != "")
                {
                    player.bowler_number = Int32.Parse(clubPlayerBowlerNo[i]);
                }

                clubBatsmenList.Add(player);

                if (player.bowler_number != null)
                {
                    clubBowlersList.Add(player);
                }

            }

            ClubOtherFixtureDetail clubOtherDetail = new ClubOtherFixtureDetail() { fixture_id = scorecardFixtureId };

            IQueryable<ClubFixturePlayer> queryableClubBatsmem = clubBatsmenList.AsQueryable().OrderBy(cfp => cfp.batsman_number);
            List<ClubFixturePlayer> clubBatsmenToReturn = queryableClubBatsmem.ToList();

            IQueryable<ClubFixturePlayer> queryableClubBowlers = clubBowlersList.AsQueryable().OrderBy(cfp => cfp.bowler_number);
            List<ClubFixturePlayer> clubBowlersToReturn = queryableClubBowlers.ToList();

            List<OpponentFixturePlayer> opponentBatsmenList = new List<OpponentFixturePlayer>();
            List<OpponentFixturePlayer> opponentBowlersList = new List<OpponentFixturePlayer>();

            for (int i = 0; i < oppPlayerBatsmanNo.Length; i++)
            {
                OpponentFixturePlayer opPlayer = new OpponentFixturePlayer();

                if (oppPlayerFixtureId[i] != "")
                {
                    opPlayer.op_fixture_id = Int32.Parse(oppPlayerFixtureId[i]);
                }

                if (oppPlayerBatsmanNo[i] != "")
                {
                    opPlayer.op_batsman_number = Int32.Parse(oppPlayerBatsmanNo[i]);
                }
                if (oppPlayerName[i] != "")
                {
                    opPlayer.op_player_name = oppPlayerName[i];
                }

                if (oppPlayerBowlerNo[i] != "")
                {
                    opPlayer.op_bowler_number = Int32.Parse(oppPlayerBowlerNo[i]);
                }

                opponentBatsmenList.Add(opPlayer);

                if (opPlayer.op_bowler_number != null)
                {
                    opponentBowlersList.Add(opPlayer);
                }

            }

            IQueryable<OpponentFixturePlayer> queryableOpponentBatsmen = opponentBatsmenList.AsQueryable().OrderBy(ofp => ofp.op_batsman_number);
            List<OpponentFixturePlayer> opponentBatsmenToReturn = queryableOpponentBatsmen.ToList();
            
            IQueryable<OpponentFixturePlayer> queryableOpponentBowlers = opponentBowlersList.AsQueryable().OrderBy(ofp => ofp.op_bowler_number);
            List<OpponentFixturePlayer> opponentBowlersToReturn = queryableOpponentBowlers.ToList();

            OpponentOtherFixtureDetail opponentOtherDetail = new OpponentOtherFixtureDetail() { fixture_id = scorecardFixtureId };

            ScorecardEnterScoresViewModel scorecard = new ScorecardEnterScoresViewModel();
            scorecard.ScorecardFixture = fixtureRepository.GetFixture(scorecardFixtureId);
            scorecard.ScorecardHeaderDetail = scorecardHeader;
            scorecard.ClubBatsmen = clubBatsmenToReturn;
            scorecard.ClubBowlers = clubBowlersToReturn;
            scorecard.ClubOtherDetail = clubOtherDetail;
            scorecard.OpponentBatsmen = opponentBatsmenToReturn;
            scorecard.OpponentBowlers = opponentBowlersToReturn;
            scorecard.OpponentOtherDetail = opponentOtherDetail;

            return scorecard;
        }

        
        
    }
}